﻿using System;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Net.Http;
using DTcms.Common;
using DTcms.Model;

namespace DTcms.Web.Mvc
{
    public class SignValidateAttribute : ActionFilterAttribute
    {
        //是否调试模式
        public bool IsDebug { get; set; }
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var request = ((HttpContextWrapper)actionContext.Request.Properties["MS_HttpContext"]).Request;
            string username_web = DTRequest.GetString("username_web");

            if (IsDebug == true)
            {
                if (request.QueryString["sign"] == "4be5fc992b577b1ec63a0aeec62f3ad1")
                {
                    //actionContext.Response = actionContext.Request.CreateResponse(new ModelResponse<string>() { ErrCode = -1, ErrMsg = "签名错误" });
                    actionContext.Response = actionContext.Request.CreateResponse(new ModelResponse<string>() { status = -1, msg = "签名错误", Response = Sign.SignRequest("?" + HttpContext.Current.Server.UrlDecode(request.QueryString.ToString())) });
                    return;
                }
                if (request.CurrentExecutionFilePath.Contains("GetQRCode"))
                {
                    //actionContext.Response = actionContext.Request.CreateResponse(new ModelResponse<string>() { ErrCode = -1, ErrMsg = "签名错误" });
                    //actionContext.Response = actionContext.Request.CreateResponse(new ModelResponse<string>() { ErrCode = -1, ErrMsg = "签名错误", Response = Sign.SignRequest("?" + HttpContext.Current.Server.UrlDecode(request.QueryString.ToString())) });
                    return;
                }
                if (request.QueryString["sign"] != Sign.SignRequest("?" + HttpContext.Current.Server.UrlDecode(request.QueryString.ToString())))
                {
                    //actionContext.Response = actionContext.Request.CreateResponse(new ModelResponse<string>() { ErrCode = -1, ErrMsg = "签名错误" });
                    actionContext.Response = actionContext.Request.CreateResponse(new ModelResponse<string>() { status = -1, msg = "签名错误", Response = Sign.SignRequest("?" + HttpContext.Current.Server.UrlDecode(request.QueryString.ToString())) });
                    return;
                }
            }
            bool content1 = request.CurrentExecutionFilePath.ToString().Contains("GetQRCode");
            if (content1 == true)
            {
                return;
            }
            if (!string.IsNullOrEmpty(username_web))
            {
                if (Convert.ToString(CacheHelper.Get(username_web)) != request.QueryString["sign"])
                {
                    actionContext.Response = actionContext.Request.CreateResponse(new ModelResponse<string>() { status = -1, msg = "签名错误" });
                    return;
                }
                base.OnActionExecuting(actionContext);
                return;
            }
            if (request.QueryString["sign"] == Sign.SignRequest("?" + HttpContext.Current.Server.UrlDecode(request.QueryString.ToString())))
            {
                base.OnActionExecuting(actionContext);
                return;
            }
            actionContext.Response = actionContext.Request.CreateResponse(new ModelResponse<string>() { status = -1, msg = "签名错误" });
        }
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {

            base.OnActionExecuted(actionExecutedContext);
            bool content1 = actionExecutedContext.ActionContext.Request.RequestUri.ToString().Contains("GetQRCode");
            if (content1 == false)
            {
                // 取得由 API 返回的资料
                var content = actionExecutedContext.ActionContext.Response.Content.ReadAsAsync<object>().Result;

                string str = JsonHelper.ObjectToJSON(content).Replace("\"/upload/", "\"http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.ServerVariables.Get("Server_Port") + "/upload/").Replace("Response\":\"\"", "Response\":null");
                if (HttpContext.Current.Request.ServerVariables.Get("Server_Port") == "7443")
                {
                    str = JsonHelper.ObjectToJSON(content).Replace("\"/upload/", "\"http://" + HttpContext.Current.Request.Url.Host + ":7001/upload/").Replace("Response\":\"\"", "Response\":null");
                }
                HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, System.Text.Encoding.GetEncoding("UTF-8"), "application/json") };
                //结果转为自定义消息格式
                HttpResponseMessage httpResponseMessage = result;

                // 重新封装回传格式
                actionExecutedContext.Response = httpResponseMessage;
            }
        }
    }
}